import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class WordCount {

    public static void main(String[] args) {
        if (args.length < 2) {
            System.out.println("Usage: java WordCount -c|-w <input_file_name>");
            System.exit(1);
        }

        String parameter = args[0];
        String inputFileName = args[1];
        int count = 0;

        try (BufferedReader reader = new BufferedReader(new FileReader(inputFileName))) {
            if (parameter.equals("-c")) {
                count = countCharacters(reader);
            } else if (parameter.equals("-w")) {
                count = countWords(reader);
            } else {
                System.out.println("Invalid parameter. Use '-c' for character count or '-w' for word count.");
                System.exit(1);
            }
        } catch (IOException e) {
            System.out.println("Error reading file: " + e.getMessage());
            System.exit(1);
        }

        if (parameter.equals("-c")) {
            System.out.println("character number:" + count);
        } else if (parameter.equals("-w")) {
            System.out.println("word number:" + count);
        }
    }

    private static int countCharacters(BufferedReader reader) throws IOException {
        int count = 0;
        String line;
        while ((line = reader.readLine()) != null) {
            count += line.length();
        }
        return count;
    }

    private static int countWords(BufferedReader reader) throws IOException {
        int count = 0;
        String line;
        boolean inWord = false;
        while ((line = reader.readLine()) != null) {
            for (char ch : line.toCharArray()) {
                if (Character.isWhitespace(ch) || ch == ',') {
                    if (inWord) {
                        count++;
                        inWord = false;
                    }
                } else {
                    inWord = true;
                }
            }
            if (inWord) {
                count++; // Count the last word in the line
                inWord = false;
            }
        }
        return count;
    }
}
